package com.xbongbong.pro.product.pojo;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.saas.domain.entity.OutstockProductEntity;
import lombok.Getter;
import lombok.Setter;

import java.util.Objects;

/**
 * 单据保存关联产品信息
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/6/11 17:19
 * @since v1.0
 */
@Getter
@Setter
public class ProductSavePojo {
    /**
     * 子产品id(sku)
     */
    private Long id;
    /**
     * 父产品id
     */
    private Long parentId;
    /**
     * 产品数量
     */
    private Double num;
    /**
     * XXX产品表的id
     */
    private Long businessProductId;
    /**
     * 批次
     */
    private String batch = "";
    /**
     * 生产日期
     */
    private Long produceDate = 0L;
    /**
     * 保质期
     */
    private Long guaranteePeriod = 0L;
    /**
     * 备注
     */
    private String memo = "";
    /**
     * 仓库信息
     */
    private Warehouse warehouse;
    /**
     * 库存产品自定义字段
     */
    private JSONObject redundantData;

    /**
     * 折扣(合同/销售出库折扣)
     */
    private Double saleDiscount;

    /**
     * 销售出库-售价
     */
    private Double saleProductPrice;

    /**
     * 选择批次，库存临时处理
     */
    private Double productNum;

    /**
     * 上游关联产品表id
     */
    private Long refProductId;

    @Setter
    @Getter
    public static class Warehouse{
        /**
         * 仓库id
         */
        private Long id;
        /**
         * 仓库名称
         */
        private String value;

        public Warehouse() {
        }
    }

    public void setProduceDate(Long produceDate) {
        if (Objects.isNull(produceDate)){
            produceDate = 0L;
        }
        if (String.valueOf(produceDate).length() > 10){
            produceDate = produceDate / 1000;
        }
        this.produceDate = produceDate;
    }

    /**
     * 重写produceDate的setter方法
     * 保存的时候前端传过来的时"yyyy-MM-dd"时间格式通过反射调用这个方法转成时间戳
     *
     * @param produceDateStr yyyy-MM-dd
     * @author GuoJun.HU
     * @since V1.0
     * @date 2019/6/17 11:30
     */
    public void setProduceDate(String produceDateStr) {
        long produceDate = 0L;
        if (StringUtil.isNotEmpty(produceDateStr)){
            produceDate = DateTimeUtil.getInt(produceDateStr,DateTimeUtil.SDFDate);
        }
        this.produceDate = produceDate;
    }

    public void setGuaranteePeriod(Long guaranteePeriod) {
        if (Objects.isNull(guaranteePeriod)){
            guaranteePeriod = 0L;
        }
        this.guaranteePeriod = guaranteePeriod;
    }

    /**
     * 是否是新增数据
     * @return true是新增
     */
    public boolean isAdd(){
        return Objects.isNull(this.businessProductId) || Objects.equals(this.businessProductId,0L);
    }
}
